<template>
	<el-dialog v-model="visible" :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false">
		<el-form ref="dataFormRef" :model="dataForm" :rules="dataRules" label-width="100px" @keyup.enter="submitHandle()">
			<el-form-item label="所属机构" prop="orgId">
				<el-select v-model="dataForm.orgName" placeholder="请选择" disabled></el-select>
			</el-form-item>
			<el-form-item label="名称" prop="name">
				<el-input v-model="dataForm.name" placeholder="名称"></el-input>
			</el-form-item>
			<el-form-item label="提交方式" prop="submitType">
				<el-select v-model="dataForm.submitType" placeholder="请选择" clearable>
					<el-option v-for="item in submitOptions" :key="item.value" :label="item.label" :value="item.value" />
				</el-select>
			</el-form-item>
			<el-form-item label="文件地址" prop="path">
				<el-input
					v-model="dataForm.path"
					:rows="3"
					type="textarea"
					placeholder="/tmp/WordCount.jar"
				/>
			</el-form-item>
			<el-form-item label="主类(启动类)" prop="mainClass">
				<el-input
					v-model="dataForm.mainClass"
					:rows="3"
					type="textarea"
					placeholder="org.apache.flink.streaming.examples.wordcount.WordCount"
				/>
			</el-form-item>
			<el-form-item label="主类参数" prop="params">
				<el-input
					v-model="dataForm.params"
					type="textarea"
					:rows="3"
					placeholder="格式: --key1 value1 --key2 value2,比如 --id2 --name xxx"
				></el-input>
			</el-form-item>
			<el-form-item label="备注" prop="note">
				<el-input v-model="dataForm.note" placeholder="备注"></el-input>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible = false">取消</el-button>
			<el-button type="primary" :loading="loading" @click="submitHandle()">确定</el-button>
		</template>
	</el-dialog>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue'
import { ElMessage } from 'element-plus/es'
import store from '@/store'
import { useDictProviceApi } from '@/api/sys/dict'
import { useJarPackageApi, useJarPackageSubmitApi, deleteJarPackageApi, jarPageApi, jarRunTypeApi } from '@/api/data-development/jarpackageManage'

const emit = defineEmits(['refreshDataList'])

const visible = ref(false)
const dataFormRef = ref()

const dataForm = reactive({
	orgId: store.userStore.user.orgId,
	orgName: store.userStore.user.orgName,
	name: '',
	submitType: '',
	path: '',
	mainClass: '',
	params: '',
	note: ''
})
const submitOptions = ref([])

const loading = ref(false)

const init = (id?: number) => {
	getdict()
	visible.value = true
	dataForm.id = ''

	// 重置表单数据
	if (dataFormRef.value) {
		dataFormRef.value.resetFields()
	}
	if (id) {
		getJarPackage(id)
	}
}

const getJarPackage = (id: number) => {
	useJarPackageApi(id).then(res => {
		Object.assign(dataForm, res.data)
	})
}

// 获取字典
const getdict = () => {
	const params = {
		page: 1,
		limit: 1000,
		dictType: 'flink_jar_submit_type',
		patientId: 0
	}
	let str = ''
	for (let key in params) {
		if (str) {
			str = str + '&' + key + '=' + params[key]
		} else {
			str = key + '=' + params[key]
		}
	}
	useDictProviceApi(str).then(res => {
		if (res.data.list.length > 0) {
			res.data.list.forEach(item => {
				item.label = item.dictLabel
				item.value = Number(item.dictValue)
			})
		}
		submitOptions.value = res.data.list
	})
}

const dataRules = ref({
	orgName: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
	name: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
	submitType: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
	path: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
	mainClass: [{ required: true, message: '必填项不能为空', trigger: 'blur' }]
})

// 表单提交
const submitHandle = () => {
	dataFormRef.value.validate((valid: boolean) => {
		if (!valid) {
			return false
		}

		loading.value = true
		useJarPackageSubmitApi(dataForm)
			.then(() => {
				ElMessage.success({
					message: '操作成功',
					duration: 500,
					onClose: () => {
						loading.value = false
						visible.value = false
						emit('refreshDataList')
					}
				})
			})
			.catch(err => {
				loading.value = false
			})
	})
}

defineExpose({
	init
})
</script>
